import java.util.Scanner;

public class Leetcode198 {
    public static void main(String[] args) {

        System.out.println(rob(new int[]{1, 2, 3, 1}));
    }

    public static int rob(int[] nums) {
        if (nums.length == 1) {
            return nums[0];
        }

        if (nums.length == 2) {
            return Math.max(nums[0], nums[1]);
        }

        int[] currentIndexMax = new int[nums.length];

        currentIndexMax[0] = nums[0];
        currentIndexMax[1] = Math.max(nums[0], nums[1]);
        for (int i = 2; i < nums.length; i++) {
            currentIndexMax[i] = Math.max(currentIndexMax[i - 1], currentIndexMax[i - 2] + nums[i]);
        }

        return currentIndexMax[nums.length - 1];
    }

}

